package com.manager.devicemanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.manager.devicemanager.entity.DeviceInfo;
import com.manager.devicemanager.entity.DeviceUser;
import com.manager.devicemanager.entity.UserInfo;
import com.manager.devicemanager.mapper.DeviceInfoMapper;
import com.manager.devicemanager.service.IDeviceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manager.devicemanager.service.IDeviceUserService;
import com.manager.devicemanager.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author wpf
 * @since 2025-05-27
 */
@Service
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements IDeviceInfoService {
    @Autowired
    private IDeviceUserService identityUserService;
    @Autowired
    private IUserInfoService iUserInfoService;
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Override
    public void batchInsertOnDuplicateKeyUpdate(Collection<DeviceInfo> list) {
        baseMapper.batchInsertOnDuplicateKeyUpdate(list);
    }

    @Override
    public void batchInsertOnDuplicateKeyIgnore(Collection<DeviceInfo> list) {
        baseMapper.batchInsertOnDuplicateKeyIgnore(list);
    }

    @Override
    public List<UserInfo> getDeviceUserList(String deviceid) {
        LambdaQueryWrapper<DeviceUser>   queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceUser::getDeviceid, deviceid);
        List<DeviceUser> list = identityUserService.list(queryWrapper);
        LambdaQueryWrapper<UserInfo> userInfoWrapper = new LambdaQueryWrapper<>();
        List<String> collect = list.stream().map(DeviceUser::getUserid).collect(Collectors.toList());
        if(collect.isEmpty()){
            return new ArrayList<>();
        }
        userInfoWrapper.in(UserInfo::getId, collect);
        List<UserInfo> list1 = iUserInfoService.list(userInfoWrapper);
        return list1;

    }

    @Override
    public List<DeviceInfo> getDeviceListByType(String type) {
        LambdaQueryWrapper<DeviceInfo>   queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceInfo::getType, type);
        List<DeviceInfo> list = deviceInfoMapper.selectList(queryWrapper);
        return  list;
    }

}
